package com.accp.controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.accp.common.DataResult;
import com.accp.entity.Users;
import com.accp.service.UsersService;

@RestController
@RequestMapping("/user")
public class UserController {

	@Autowired
	UsersService usersService;
	
	@GetMapping("/login")
	public DataResult login(String username,String password,HttpSession session){
		Users user = this.usersService.login(username, password);
		if(user == null){
			return DataResult.getErrorResult(303, null, "账号或密码错误");
		}else{
			session.setAttribute("user", user);
			return DataResult.getSuccessResult(user);
		}
	}
	
	@GetMapping("/autologin")
	public DataResult autologin(HttpSession session){
		Users user = (Users) session.getAttribute("user");
		if(user == null){
			return DataResult.getErrorResult(303, null, "还没有登录");
		}else{
			return DataResult.getSuccessResult(user);
		}
	}
	
	@GetMapping("/logout")
	public DataResult logout(HttpSession session){
		//让session失效
		session.invalidate();
		return DataResult.getSuccessResult(null);
	}
}
